package com.control.sqlitemanager;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.control.dao.BaseSqliteDao;
import com.control.model.CookerModel;
import com.control.sqlitemanager.CookerConfigInfoManager.CookerModelInfo_Column;

public final class CookerConfigManager extends BaseSqliteDao<CookerModel>{

	
	private  Context mContext;
	
	private SQLiteDatabase mysqlite;
	
	public enum CookerModel_Column{
		cooker_model,
		id,
		name,
		Cooker_id,
		img_url;
	}
	
	public CookerConfigManager(Context context) {
		super(context);
		mContext = context;
	}
	public List<CookerModel>  fetchAllData(Map<String,String> param){
		List<CookerModel> lists = new ArrayList<CookerModel>();
		try{
			String[] args = new String[1];
			if(param!=null){
				args[0] = param.get(String.valueOf(CookerModel_Column.Cooker_id));
			}else{
				args[0] = "0";
			}
			mysqlite = this.getReadableDatabase();
			Cursor cursor =	mysqlite.query(String.valueOf(CookerModel_Column.cooker_model), 
					new String[]{
						String.valueOf(CookerModel_Column.id),
						String.valueOf(CookerModel_Column.Cooker_id),
						String.valueOf(CookerModel_Column.img_url),
						String.valueOf(CookerModel_Column.name)
					}, 
					"Cooker_id=?",args , null, null, " id asc");
			
			CookerModel cooker = null;
			while(cursor.moveToNext()){
				String id = cursor.getString(cursor.getColumnIndex(String.valueOf(CookerModel_Column.id)));
				String cookerId = cursor.getString(cursor.getColumnIndex(String.valueOf(CookerModel_Column.Cooker_id)));
				String imgUrl = cursor.getString(cursor.getColumnIndex(String.valueOf(CookerModel_Column.img_url)));
				String name = cursor.getString(cursor.getColumnIndex(String.valueOf(CookerModel_Column.name)));
				
				cooker = new CookerModel();
				cooker.setId(Integer.parseInt(id));
				cooker.setName(name);
				cooker.setCookerId(Integer.parseInt(cookerId));
				cooker.setImgUrl(imgUrl);
				lists.add(cooker);
				cooker = null;
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(mysqlite!=null){
				mysqlite.close();
			}
		}
		return lists;
	}
	
	
	public List<CookerModel>  fetchAll(){
		List<CookerModel> lists = new ArrayList<CookerModel>();
		try{
			mysqlite = this.getReadableDatabase();
			Cursor cursor =	mysqlite.query(String.valueOf(CookerModel_Column.cooker_model), 
					new String[]{
						String.valueOf(CookerModel_Column.id),
						String.valueOf(CookerModel_Column.Cooker_id),
						String.valueOf(CookerModel_Column.img_url),
						String.valueOf(CookerModel_Column.name)
					}, 
					 null,null , null, null, " id desc","1");
			
			CookerModel cooker = null;
			while(cursor.moveToNext()){
				String id = cursor.getString(cursor.getColumnIndex(String.valueOf(CookerModel_Column.id)));
				String cookerId = cursor.getString(cursor.getColumnIndex(String.valueOf(CookerModel_Column.Cooker_id)));
				String imgUrl = cursor.getString(cursor.getColumnIndex(String.valueOf(CookerModel_Column.img_url)));
				String name = cursor.getString(cursor.getColumnIndex(String.valueOf(CookerModel_Column.name)));
				
				cooker = new CookerModel();
				cooker.setId(Integer.parseInt(id));
				cooker.setName(name);
				cooker.setCookerId(Integer.parseInt(cookerId));
				cooker.setImgUrl(imgUrl);
				lists.add(cooker);
				cooker = null;
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(mysqlite!=null){
				mysqlite.close();
			}
		}
		return lists;
	}
	
	public void insert(ContentValues cv){
		try {
			mysqlite = this.getWritableDatabase();
			mysqlite.insert(String.valueOf(CookerModel_Column.cooker_model), String.valueOf(CookerModel_Column.id), cv);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(mysqlite!=null){
				mysqlite.close();
			}
		}
	}
	
	public void update(ContentValues cv){
		try {
			mysqlite = this.getWritableDatabase();
			mysqlite.update(String.valueOf(CookerModel_Column.cooker_model),cv , "id=?", new String[]{String.valueOf(cv.get(String.valueOf(CookerModelInfo_Column.id)))});
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(mysqlite!=null){
				mysqlite.close();
			}
		}
	}
	
	public void deleteRowId(int rowId){
		try {
			mysqlite = this.getWritableDatabase();
			mysqlite.delete(String.valueOf(CookerModel_Column.cooker_model), "id=?", new String[]{String.valueOf(rowId)});
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(mysqlite!=null){
				mysqlite.close();
			}
		}
	}
	
	public void bathInsert(List<ContentValues> cvs){
		try{
			mysqlite = this.getWritableDatabase();
			mysqlite.beginTransaction();
			for(int i=0,l=cvs.size();i<l;i++){
				ContentValues cv = cvs.get(i);
				mysqlite.insert(String.valueOf(CookerModel_Column.cooker_model), null, cv);
			}
			mysqlite.setTransactionSuccessful();
			mysqlite.endTransaction();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(mysqlite!=null){
				mysqlite.close();
			}
		}
	}

}
